package com.milhouz.quizzdroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.milhouz.quizzdroid.bean.Game;
import com.milhouz.quizzdroid.bean.Game.GameMode;
import com.milhouz.quizzdroid.bean.User;
import com.milhouz.quizzdroid.db.QuizzDroidDbAPI;
import com.milhouz.quizzdroid.view.AvatarUtil;

public class MainMenuActivity extends Activity {

	private Context mContext;

	private Button mSinglePlayerButton;
	private Button mMultiPlayerButton;
	private TextView mMenuText;
	private TextView mMenuText2;
	private ImageView mAvatar;

	public enum MenuItem {
		SINGLEPLAYER, MULTIPLAYER
	}

	@Override
	public void onBackPressed() {
		// Exit Application
		moveTaskToBack(true);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main);
		mContext = this.getApplicationContext();

		// Button to access Singleplayer Screen
		mSinglePlayerButton = (Button) findViewById(R.id.singlePlayerButton);
		mSinglePlayerButton.setOnClickListener(new OnMenuClickListener(MenuItem.SINGLEPLAYER));
		// Button to access Multiplayer Screen
		mMultiPlayerButton = (Button) findViewById(R.id.multiPlayerButton);
		mMultiPlayerButton.setOnClickListener(new OnMenuClickListener(MenuItem.MULTIPLAYER));

		// Set user stats
		mMenuText = (TextView) findViewById(R.id.menuText);
		mMenuText2 = (TextView) findViewById(R.id.menuText2);
		mAvatar = (ImageView) findViewById(R.id.avatar);
		setUserStats();

		// Let's activate dithering for the background to prevent banding
		Drawable d = getResources().getDrawable(R.drawable.ss_background);
		d.setDither(true);
		findViewById(android.R.id.content).setBackgroundDrawable(d);
	}

	@Override
	protected void onResume() {
		setUserStats();
		super.onResume();
	}

	private void setUserStats() {
		User user = QuizzDroidDbAPI.getInstance().getDefaultUser(this);
		mMenuText.setText(String.format(getString(R.string.game_stats_nbreponses), user.getNbResponses(), user.getTotalQuestions()));
		mMenuText2.setText(String.format(getString(R.string.game_stats_meantime), user.getMeanTimeSec()));
		// Set the avatar
		mAvatar.setBackgroundDrawable(AvatarUtil.getAvatarFromStats(this, user.getNbResponses(), user.getTotalQuestions()));

	}

	class OnMenuClickListener implements View.OnClickListener {

		private final MenuItem mMenuClicked;

		public OnMenuClickListener(MenuItem menuClicked) {
			mMenuClicked = menuClicked;
		}

		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (mMenuClicked) {
			case SINGLEPLAYER:
				intent = new Intent(mContext, GameOptionActivity.class);
				intent.putExtra(getString(R.string.intent_extra_game), new Game(GameMode.SINGLE));
				break;
			case MULTIPLAYER:
				intent = new Intent(mContext, GameOptionActivity.class);
				intent.putExtra(getString(R.string.intent_extra_game), new Game(GameMode.MULTIPLAYER, true, true, 20000, 2));
				break;
			}
			if (intent != null) {
				startActivity(intent);
			}
		}
	}

}
